//package com.example.myapplication.previewvideo;
//
//import android.Manifest;
//import android.app.Activity;
//import android.content.Context;
//import android.content.pm.PackageManager;
//import android.graphics.SurfaceTexture;
//import android.hardware.camera2.CameraAccessException;
//import android.hardware.camera2.CameraCaptureSession;
//import android.hardware.camera2.CameraCharacteristics;
//import android.hardware.camera2.CameraDevice;
//import android.hardware.camera2.CameraManager;
//import android.hardware.camera2.CameraMetadata;
//import android.hardware.camera2.CaptureRequest;
//import android.hardware.camera2.params.StreamConfigurationMap;
//import android.media.MediaRecorder;
//import android.os.Build;
//import android.os.Bundle;
//import android.os.Handler;
//import android.os.Looper;
//import android.util.Log;
//import android.util.Size;
//import android.view.Surface;
//import android.view.TextureView;
//import android.view.View;
//import android.widget.ImageButton;
//import android.widget.Toast;
//
//import androidx.annotation.NonNull;
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.core.app.ActivityCompat;
//
//import com.blankj.utilcode.util.AppUtils;
//import com.blankj.utilcode.util.LogUtils;
//import com.blankj.utilcode.util.Utils;
//import com.example.myapplication.R;
//import com.example.myapplication.previewvideo.util.CameraUtils;
//import com.example.myapplication.previewvideo.util.VideoComposer;
//
//import java.io.File;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.Comparator;
//import java.util.Date;
//import java.util.List;
//
//public class Camera2_A_VideoCaptureActivity extends AppCompatActivity {
//
//    private static final String TAG = "Camera2VideoCaptureActivity";
//
//    private static final int REQUEST_CAMERA_PERMISSION = 1;
//
//    private CameraManager cameraManager;
//    private String cameraId;
//    private CameraDevice cameraDevice;
//    private CameraCaptureSession captureSession;
//    private Size previewSize;
//
//    private MediaRecorder mediaRecorder;
//    private boolean isRecording = false;
//
//    private TextureView textureView;
//    private ImageButton btnRecord;
//
//    int desiredWidth = 1920;
//    int desiredHeight = 1080;
//
//    int recordTime = 5 * 1000;//秒
//    int fps = 30;
//    private Handler handler;
//    //1Mbps等于每秒传输1兆比特（即1,000,000比特
//    //    int bitRate = 100 * 1000000;// 100 Mbps
//    int bitRate = 5000 * 1000;//  Mbps
//
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_camera2videocapture);
//
//        textureView = findViewById(R.id.textureView);
//        btnRecord = findViewById(R.id.btnRecord);
//
//        btnRecord.setOnClickListener(v -> {
//            if (isRecording) {
//                stopRecording();
//            } else {
//                startRecording();
//
//            }
//        });
//
//        findViewById(R.id.btnRecordhb).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                startCompose();
//            }
//        });
//
//
//
//
//
//        cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
//        textureView.setSurfaceTextureListener(surfaceTextureListener);
//        handler = new Handler(Looper.getMainLooper());
//
//
//
//
//
//    }
//
//
//    private void startRecording() {
//        if (isRecording) return;
//
//        try {
//            setupMediaRecorder();
//            cameraDevice.createCaptureSession(Arrays.asList(new Surface(mediaRecorder.getSurface())), new CameraCaptureSession.StateCallback() {
//                @Override
//                public void onConfigured(@NonNull CameraCaptureSession session) {
//                    cameraCaptureSession = session;
//                    try {
//                        cameraCaptureSession.setRepeatingRequest(createCaptureRequest(), null, null);
//                        mediaRecorder.start();
//                        isRecording = true;
//                        btnStart.setEnabled(false);
//                        btnStop.setEnabled(true);
//                        Toast.makeText(MainActivity.this, "录制开始", Toast.LENGTH_SHORT).show();
//
//                        // 设置一个定时器自动停止录制并重启
//                        textureView.postDelayed(() -> {
//                            stopRecording();
//                            startRecording(); // 立即开始新的录制
//                        }, 10000); // 10秒录制
//
//                    } catch (CameraAccessException | IllegalStateException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                @Override
//                public void onConfigureFailed(@NonNull CameraCaptureSession session) {
//                    Toast.makeText(MainActivity.this, "配置失败", Toast.LENGTH_SHORT).show();
//                }
//            }, null);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void stopRecording() {
//        if (!isRecording) return;
//
//        try {
//            cameraCaptureSession.stopRepeating();
//            mediaRecorder.stop();
//            mediaRecorder.reset();
//            btnStart.setEnabled(true);
//            btnStop.setEnabled(false);
//            Toast.makeText(this, "录制停止，视频保存至：" + videoFilePath, Toast.LENGTH_SHORT).show();
//            isRecording = false;
//        } catch (IllegalStateException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void setupMediaRecorder() throws IOException {
//        videoFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/video_" + System.currentTimeMillis() + ".mp4";
//
//        mediaRecorder = new MediaRecorder();
//        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
//        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//        mediaRecorder.setVideoSize(1280, 720);
//        mediaRecorder.setVideoFrameRate(30);
//        mediaRecorder.setVideoEncodingBitRate(1000000);
//        mediaRecorder.setOutputFile(videoFilePath);
//        mediaRecorder.prepare();
//    }
//
//    private CaptureRequest createCaptureRequest() {
//        try {
//            CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
//            Surface previewSurface = new Surface(textureView.getSurfaceTexture());
//            builder.addTarget(previewSurface);
//            builder.addTarget(mediaRecorder.getSurface());
//            return builder.build();
//        } catch (CameraAccessException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//    private TextureView.SurfaceTextureListener surfaceTextureListener = new TextureView.SurfaceTextureListener() {
//        @Override
//        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
//            startCamera();
//        }
//
//        @Override
//        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
//
//        @Override
//        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
//            return false;
//        }
//
//        @Override
//        public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
//    };
//
//    private void startCamera() {
//        CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
//        try {
//            String cameraId = cameraManager.getCameraIdList()[0];
//            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//                return;
//            }
//            cameraManager.openCamera(cameraId, new StateCallback() {
//                @Override
//                public void onOpened(@NonNull CameraDevice camera) {
//                    cameraDevice = camera;
//                }
//
//                @Override
//                public void onDisconnected(@NonNull CameraDevice camera) {
//                    camera.close();
//                }
//
//                @Override
//                public void onError(@NonNull CameraDevice camera, int error) {
//                    camera.close();
//                }
//            }, null);
//        } catch (CameraAccessException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private boolean hasPermissions() {
//        return ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED &&
//                ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
//                ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
//    }
//
//    @Override
//    protected void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        if (requestCode == REQUEST_PERMISSIONS) {
//            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                // 权限已被授予
//            } else {
//                Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
//            }
//        }
//    }
//
//
//}
//
